home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 507 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Strange (to me) notation - little help?
  5. Date: 4 Jan 1996 22:06:43 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4chj1j$384@colossus.holonet.net>
  8. References: <Pine.SOL.3.91.960103225557.22729H-100000@qew.cs>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Rimon Barr (barr@vis.toronto.edu) wrote:
  13. : >  SimpleCat::SimpleCat(int age, int weight):
  14. : >  itsAge(age), itsWeight(weight) {}
  15.  
  16. : Well, I haven't run the code and I never knew you could do that, but it's 
  17. : is cool and I think I know what it does. Please check this out and get 
  18. : back to me.
  19.  
  20. There's no mystery here -- it's a member-initialization list.
  21. See any C++ book.
  22.  
  23. : In C++ you can call an inherited function as follows:
  24. : Let's say you have
  25.  
  26. : class a {
  27. :   a(int num1, int num2);
  28. : };
  29. : class b:public a {
  30. :   b(int num1): a(num1, 0);
  31.                  ^^^^^^^^^^
  32. Wrong syntax -- you're missing b::b's function body, which is
  33. required when you specify a member-initialization list, e.g.
  34.  
  35.     b(int num1): a(num1, 0) { /* something */ } 
  36.  
  37. and of course, you need to make a::a(int, int) public or
  38. protected for your code to compile.
  39.  
  40. Perhaps more importantly, you might want to ponder the
  41. difference between an initialization (which the above is)
  42. and a function call (which the above is not).  Granted, the
  43. compiler must invoke a::a(int,int), but it may need to do 
  44. other things as well.  In fact, it's illegal to call a ctor 
  45. as a function, anyway.
  46. ...
  47. : This possess the question of whether the following works too:
  48. : int a(10);
  49. : Would the integer a now hold the number 10?
  50.  
  51. Yes, it would.  Why not try it out yourself?  Again, this is
  52. an initialization, not a function call.  It means exactly the
  53. same thing as   int a=10;
  54. --
  55. Russell Blackadar,   russell@mdli.com
  56.